import 'dart:math';

import 'package:demo/widget/animation/fade_animation.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class FadeAnimationList extends StatefulWidget {
  const FadeAnimationList({Key? key}) : super(key: key);

  @override
  FadeAnimationListState createState() => FadeAnimationListState();
}

class FadeAnimationListState extends State<FadeAnimationList> with TickerProviderStateMixin {
  late final AnimationController animationController;

  @override
  void initState() {
    animationController = AnimationController(duration: const Duration(seconds: 2), vsync: this);
    super.initState();
  }

  @override
  void dispose() {
    animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    const int itemLength = 20;
    return Scaffold(
      appBar: AppBar(
        title: Text("渐变动画list"),
      ),
      body: GridView(
        padding: const EdgeInsets.only(top: 12, left: 12, right: 12, bottom: 12),
        physics: const BouncingScrollPhysics(),
        scrollDirection: Axis.vertical,
        children: List<Widget>.generate(
          itemLength,
          (int index) {
            return FadeAnimation(
              stopAnimation: Random().nextBool(),
              index: index,
              length: itemLength,
              animationController: animationController,
              child: Container(
                height: 48,
                decoration: BoxDecoration(
                  borderRadius: const BorderRadius.all(Radius.circular(4.0)),
                  color: Colors.blue[700],
                ),
              ),
            );
          },
        ),
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2,
          mainAxisSpacing: 12.0,
          crossAxisSpacing: 12.0,
          childAspectRatio: 1.5,
        ),
      ),
    );
  }
}
